package com.android.infantschool.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.BindingQrCodeBean;
import com.android.infantschool.bean.ToViewBindingBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.BabyBindingQrCodePresenter;
import com.android.infantschool.model.mvp.presenter.BabyBindingQrCodePresenterImpl;
import com.android.infantschool.model.mvp.presenter.ToViewBindingPresenter;
import com.android.infantschool.model.mvp.presenter.ToViewBindingPresenterImpl;
import com.android.infantschool.model.mvp.view.BabyBindingQrCodeView;
import com.android.infantschool.model.mvp.view.ToViewBindingView;
import com.android.infantschool.ui.adapter.ToViewBindingAdapter;
import com.android.infantschool.ui.dialog.DialogParentsBinding;
import com.android.infantschool.util.ToastUtil;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ToViewBindingActivity extends BaseActivity implements BabyBindingQrCodeView, ToViewBindingView {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.recyclerView)
    public RecyclerView recyclerView;

    public ToViewBindingAdapter adapter;
    @BindView(R.id.btn_binding)
    public Button btnBinding;

    private String babyName = "";
    private String babyId = "";
    private BabyBindingQrCodePresenter qrCodePresenter = null;
    private ToViewBindingPresenter bindingPresenter = null;

    public static Intent newIntent(Activity activity, String babyName, String babyId) {
        Intent intent = new Intent(activity, ToViewBindingActivity.class);
        intent.putExtra("babyName", babyName);
        intent.putExtra("babyId", babyId);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_view_binding);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        bindingPresenter = new ToViewBindingPresenterImpl(this);
        qrCodePresenter = new BabyBindingQrCodePresenterImpl(this);
        rlTitle.setPadding(0, this.height, 0, 0);
        tvTitle.setText(getIntent().getStringExtra("babyName") + "家长");

        adapter = new ToViewBindingAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.showLoading();
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        map.put("babyId", getIntent().getStringExtra("babyId"));
        bindingPresenter.getToViewBinding(map);
        Log.e(TAG, "map:" + map.toString());
    }

    @OnClick({R.id.img_back, R.id.btn_binding})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                this.finish();
                break;
            case R.id.btn_binding:
                this.showLoading();
                Map<String, String> map = new HashMap<>();
                map.put("token", AppConfig.getToken());
                map.put("babyId", getIntent().getStringExtra("babyId"));
                Log.e(TAG, "map:" + map.toString());
                qrCodePresenter.getBabyBindingQrCode(map);
                break;
        }
    }

    private DialogParentsBinding dialogParentsBinding = null;

    private void showParentsBindingDialog(String url) {
        dismissParentsBindingDialog();
        dialogParentsBinding = new DialogParentsBinding(this, url);
        dialogParentsBinding.show();
    }

    private void dismissParentsBindingDialog() {
        if (dialogParentsBinding != null) {
            dialogParentsBinding.dismiss();
            dialogParentsBinding.cancel();
            dialogParentsBinding = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bindingPresenter != null) {
            bindingPresenter.onDestroy();
            bindingPresenter = null;
        }
        if (qrCodePresenter != null) {
            qrCodePresenter.onDestroy();
            qrCodePresenter = null;
        }
        dismissParentsBindingDialog();
    }

    @Override
    public void errorBabyBindingQrCode(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successBabyBindingQrCode(BaseBean baseBean) {
        this.dismissLoading();
        BindingQrCodeBean qrCodeBean = (BindingQrCodeBean) baseBean;
        if (qrCodeBean.getCode() == 0) {
            showParentsBindingDialog(qrCodeBean.getData());
        } else {
            ToastUtil.showShortToast(this, "获取失败:" + qrCodeBean.getMsg());
        }
    }

    @Override
    public void errorToViewBinding(String error) {
        this.dismissLoading();
        ToastUtil.showShortToast(this, "网络异常:" + error);
    }

    @Override
    public void successToViewBinding(BaseBean baseBean) {
        this.dismissLoading();
        ToViewBindingBean bindingBean = (ToViewBindingBean) baseBean;
        if (bindingBean.getCode() == 0) {
            adapter.setData(bindingBean.getData());
        } else {
            ToastUtil.showShortToast(this, "获取失败:" + bindingBean.getMsg());
        }
    }
}